import { useState } from "react";
import { View } from "@tarojs/components";
import Taro, { useUnload, useDidShow } from "@tarojs/taro";
import { getSchoolSubjectList } from "@/axios/common";
import { ITab, Footer } from "@/components/index";
import AOVIndex from "../components/AOV/index";
import ELPIndex from "../components/ELP/index";
import MLPIndex from "../components/MLP/index";
import PTIndex from "../components/PT/index";
import MIIndex from "../components/MI/index";
import SIIndex from "../components/SI/index";
import OIIndex from "../components/OI/index";

import "./index.scss";

const tabsMap = [
  {
    label: "测评概览",
    value: 0,
    id: 0,
    components: <AOVIndex />,
  },
  {
    label: "英语能力情况",
    value: 1,
    id: 5,
    components: <ELPIndex />,
  },
  {
    label: "数学能力情况",
    value: 2,
    id: 6,
    components: <MLPIndex />,
  },
  {
    label: "性格类型情况",
    value: 3,
    id: 1,
    components: <PTIndex />,
  },
  {
    label: "多元智能",
    value: 4,
    id: 4,
    components: <MIIndex />,
  },
  {
    label: "学科兴趣",
    value: 5,
    id: 2,
    components: <SIIndex />,
  },
  {
    label: "职业兴趣",
    value: 6,
    id: 3,
    components: <OIIndex />,
  },
];

export default function Index() {
  const [currentTab, setCurrentTab] = useState<number>(0);
  const [tabsData, setTabsData] = useState<any[]>([]);

  const getSchoolSubjectData = async () => {
    try {
      const results = await getSchoolSubjectList();
      const ids: any[] = results.map((item) => item.subjectId);
      const result: any[] = [];
      tabsMap.map((item) => {
        if (ids.includes(item.id) || item.id == 0) result.push(item);
      });
      setTabsData(result);
    } catch {}
  };

  useDidShow(() => {
    getSchoolSubjectData();
    const classRes: API.ClassAdminItem = Taro.getStorageSync("CLASSRES");
    if (Object.keys(classRes).length > 0) {
      Taro.setNavigationBarTitle({
        title: classRes.gradeName + " " + classRes.className,
      });
    }
  });

  useUnload(() => {
    Taro.removeStorageSync("CLASSRES");
  });

  return (
    <View className="index">
      <ITab
        tabData={tabsData}
        swipe
        currentTab={currentTab}
        onChangeTab={(value: number) => setCurrentTab(value)}
      />
      <Footer />
    </View>
  );
}
